/**
 * Data object for a user of the system. This
 * should be subclassed for all specific types
 * of users
 */
package ca.lscs.model;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;

import javax.persistence.Id;

import org.apache.geronimo.mail.util.Base64Encoder;

/**
 * @author Steve Pennington
 *
 */
public class AppUser implements Serializable {
	private static final long serialVersionUID = 3566372795674625141L;
	
	@Id
	private Long id;
	private String username;
	private String password;
	private String firstName;
	private String lastName;
	private String salt;
	private UserType type;
	
	//Needed for objectify
	public AppUser() {}
	
	public AppUser(String username, String firstName, String lastName, String password, UserType type) throws NoSuchAlgorithmException, IOException {
		this.setUsername(username);
		this.setPassword(password);
		this.firstName = firstName;
		this.lastName = lastName;
		this.type = type;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public static String createPassword(String password) throws NoSuchAlgorithmException, IOException {
		MessageDigest md = MessageDigest.getInstance("SHA-512");
		byte[] bytes = md.digest(password.getBytes());
		Base64Encoder encoder = new Base64Encoder();
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		int result = encoder.encode(bytes, 0, bytes.length, outStream);
		String s = outStream.toString();
		return s;
	}

	public void setPassword(String password) throws NoSuchAlgorithmException, IOException {
		this.password = createPassword(password);
	}

	public String getPassword() {
		return password;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}

	public void setType(UserType type) {
		this.type = type;
	}

	public UserType getType() {
		return type;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}
	
}
